

#include "TextQuery.h"


////////////////////////////////
//        TextQuery           //
////////////////////////////////




void TextQuery::loadFile(ifstream &infile) {
	string line, word;
	line_no row = 0;
	while (getline(infile, line)) {
		lpLinesVec->push_back(line);
		istringstream iss(line);
		while (iss >> word) {
			auto &wSet = wordRowsMap[word];
			if (!wSet) {
				wSet.reset(new set<line_no>);
			}
			wSet->insert(row);
		}
		++row;
	}
}


QueryResult TextQuery::query(const string &s) const {
	static shared_ptr<set<line_no>> nodata(new set<line_no>);
	auto res = wordRowsMap.find(s);
	if (res == wordRowsMap.end()) {
		return QueryResult(s, nodata, lpLinesVec);
	} else {
		return QueryResult(s, res->second, lpLinesVec);
	}

}